package com.hn.gege.sys.modules.sys.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.sys.modules.sys.entity.DictDetail;
import com.hn.gege.sys.modules.sys.mapper.DictDetailMapper;
import com.hn.gege.sys.modules.sys.service.IDictDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 字典明细表 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-08-06
 */
@Service
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDetail> implements IDictDetailService {

    @Override
    public boolean save(DictDetail entity) {
        entity.setPy(PinyinUtil.getFirstLetter(entity.getDictName(),""));
        return super.save(entity);
    }


    @Override
    public DictDetail getByPk(String dictType, String dictCode) {
        LambdaQueryWrapper<DictDetail> lambda = new QueryWrapper<DictDetail>().lambda()
                .eq(DictDetail::getDictType, dictType)
				.eq(DictDetail::getDictCode, dictCode);
        return baseMapper.selectOne(lambda);
    }

    @Override
    public boolean updateByPk(DictDetail dictDetail) {
        LambdaQueryWrapper<DictDetail> lambda = new QueryWrapper<DictDetail>().lambda()
                .eq(DictDetail::getDictType, dictDetail.getDictType())
				.eq(DictDetail::getDictCode, dictDetail.getDictCode());

        if (! StringUtils.isEmpty(dictDetail.getDictName()))
            dictDetail.setPy(PinyinUtil.getFirstLetter(dictDetail.getDictName(),""));

        return super.update(dictDetail, lambda);
    }

    @Override
    public boolean removeByPk(String dictType, String dictCode) {
        LambdaQueryWrapper<DictDetail> lambda = new QueryWrapper<DictDetail>().lambda()
                .eq(DictDetail::getDictType, dictType)
				.eq(DictDetail::getDictCode, dictCode);
        return super.remove(lambda);
    }

    @Override
    public boolean updateState(String dictType, String dictCode, String state) {
        LambdaUpdateWrapper<DictDetail> lambda = new UpdateWrapper<DictDetail>().lambda()
                .set(DictDetail::getState, state)
                .eq(DictDetail::getDictType, dictType)
                .eq(DictDetail::getDictCode, dictCode);
        return super.update(lambda);
    }


}
